package com.yalemang.framework.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.yalemang.framework.utils.ViewBindingUtils

abstract class BaseFragment<VB: ViewBinding, P : BasePresenter<*, *>> : Fragment(), BaseView {

    protected lateinit var mPresenter : P
    protected lateinit var mBinding: VB

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = ViewBindingUtils.getViewBinding(
            this,
            inflater,
            container,
            false
        )!!
        initMVP()
        initData()
        initView()

        return mBinding.root
    }

    fun startCheckFileReadWritePermission(callback: (Boolean)->Unit) {
        val activity = requireActivity()
        if (activity is BaseActivity<*, *>) {
            activity.startCheckFileReadWritePermission(callback)
        }
    }

    /**
     * 使用生命周期方式进行回调，不需要重写Activity处理权限相关的回调函数
     */
    fun startCheckPermission(permissionList: MutableList<String>,callback: (Boolean) -> Unit) {
        val activity = requireActivity()
        if (activity is BaseActivity<*, *>) {
            activity.startCheckPermission(permissionList, callback)
        }
    }

    protected abstract fun initData()

    protected abstract fun initView()

    protected abstract fun initMVP()
}